//
//  KSStartSortGoodsLoadedViewController.swift
//  Supermarket
//
//  Created by liuxm on 2017/9/30.
//  Copyright © 2017年 kushao. All rights reserved.
//

import UIKit

/// 开始分拣 - 已装箱
class KSStartSortGoodsLoadedViewController: KSBaseViewController,ViewChooseCallBackDelegate,KSCompleteOrderPickingDelegate {

    @IBOutlet weak var tableView: UITableView!
    var orderId: String = ""
    private var orderArray: [KSStorageFjDetailListDataModel] = []
    private var goodsArray: [KSStorageFjDetailListGoodsModel] = []
    private var orderNumber: [String] = []
    private let chooseView = KSChooseCodeView()
    private var selectrow = 0
    @IBOutlet weak var chooseOrderBtn: UIButton!
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.title = "开始分拣"
        self.tableView.delegate = self
        self.tableView.dataSource = self
        self.tableView.backgroundColor = UIColor.colorWithHexString(hex: "#f4f5f7")
        
        let nib = UINib(nibName: "KSPickingGoodsLoadedTableViewCell", bundle: nil)
        self.tableView.register(nib, forCellReuseIdentifier: "KSPickingGoodsLoadedTableViewCell")
        chooseView.delegate = self
        self.view.addSubview(chooseView)
        chooseView.snp.makeConstraints { (make) in
            make.top.equalTo(50)
            make.left.right.equalTo(0)
            make.bottom.equalTo(0)
        }
        
        self.requestData()
    }

    private func requestData() {
        self.request.requestIosStorageFjlistdetail(fjorderId: orderId)
    }
    
    override func requestSuccess(result: String, apiType: KSApiType) {
        super.requestSuccess(result: result, apiType: apiType)
        let model = KSStorageFjDetailListModel.deserialize(from: result)
        self.orderArray = (model?.data)!
        if orderArray.count > 0 {
            self.goodsArray = (orderArray[selectrow].order_goods)!
        }
        for i in 0...orderArray.count - 1 {
            let detailModel = orderArray[i]
            orderNumber.append(detailModel.order_number!)
        }
        tableView.reloadData()
    }
    
    override func requestFaile(code: Int, message: String, apiType: KSApiType) {
        super.requestFaile(code: code, message: message, apiType: apiType)
    }
    
    func completeOrdePicking() {
        
    }
    
    @IBAction func giveUpSortOutEvent(_ sender: Any) {
        self.request.requestOrderDiscardGain(orderIds: "")
    }
    
    @IBAction func sortoutEvent(_ sender: Any) {
        let vc = KSStartPickingGoodsViewController()
        vc.orderArray = orderArray
        vc.selectrow = selectrow
        vc.delegate = self
        vc.viewControllerClass = "KSStartSortGoodsLoadedViewController"
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
    @IBAction func completeSortOutEvent(_ sender: Any) {
        
    }
    
    @IBAction func chooseOrderEvent(_ sender: Any) {
        self.chooseOrderBtn.isSelected = !self.chooseOrderBtn.isSelected
        if self.chooseOrderBtn.isSelected {
            if chooseView.dataArray.count > 0 {
                chooseView.isHidden = false
            } else {
                chooseView.dataArray = orderNumber
                chooseView.setViewALLtypeUI(width: KSConstants.kScreenWidth)
            }
        } else {
            chooseView.isHidden = true
        }
    }
    func viewChooseCallBack(selectRow: NSInteger) {
        //self.orderNoBtn.isSelected = false
        selectrow = selectRow
        goodsArray = orderArray[selectrow].order_goods!
        tableView.reloadData()
    }

}

extension KSStartSortGoodsLoadedViewController: UITableViewDelegate, UITableViewDataSource {
    
    // UITableViewDataSource
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return self.goodsArray.count
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    
    // UITableViewDelegate
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 1
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 260
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.01
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "KSPickingGoodsLoadedTableViewCell", for: indexPath) as! KSPickingGoodsLoadedTableViewCell
        cell.setDataWithModel(model: self.goodsArray[indexPath.row])
        return cell
    }
    
}
